package com.winai.launcher.view.timeManager;

import android.app.KeyguardManager;
import android.app.Service;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.os.Vibrator;
import android.text.TextUtils;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.winai.launcher.BroadcastReceiver.SystemEventReceiver;
import com.winai.launcher.R;
import com.winai.launcher.base.BaseActivity;
import com.winai.launcher.common.GlobalPublisher;
import com.winai.launcher.common.SignalSlot;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@RequiresApi(api = Build.VERSION_CODES.O_MR1)
public class AlarmAlertActivity extends BaseActivity {
    private Context mContext = this;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_remark)
    TextView tvRemark;

    private MediaPlayer mediaPlayer;
    private Vibrator vibrator;
    private String alarmId, alarmTime, alarmRemark;
    private AudioManager mAudioManager;
    private int currentVolume;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarm_alert);
        ButterKnife.bind(this);
        start();
    }

    private void start() {
        initObject();
        initView();
        config();
        initListener();
        //获取当前音量值
        currentVolume = SystemEventReceiver.mIndex;
        //如果当前音量值为<12，则设置为12.再播放闹铃音乐
        if (currentVolume < 12) {
            new Thread(() -> {
                //增大音量到12
                mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 12, 0);
            }).start();
        }
        mediaPlayer.setLooping(true);
        mediaPlayer.start();
    }

    /**
     * 说明：配置屏幕亮起
     */
    private void config() {
        setTurnScreenOn(true);
        setShowWhenLocked(true);

        KeyguardManager systemService = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
        systemService.requestDismissKeyguard(this, null);
    }

    private void initObject() {
        alarmId = getIntent().getStringExtra("alarmId");
        alarmTime = getIntent().getStringExtra("alarmTime");
        alarmRemark = getIntent().getStringExtra("alarmRemark");
        alarmTime = alarmTime == null ? "--:--" : alarmTime;
        alarmRemark = alarmRemark == null ? "闹钟" : alarmRemark;

        mAudioManager = (AudioManager) mContext.getSystemService(Service.AUDIO_SERVICE);

        mediaPlayer = MediaPlayer.create(this, R.raw.clockmusic);
        vibrator = (Vibrator) this.getSystemService(Service.VIBRATOR_SERVICE);
        //数组参数意义：第一个参数为等待指定时间后开始震动，震动时间为第二个参数。后边的参数依次为等待震动和震动的时间
        //第二个参数为重复次数，-1为不重复，0为一直震动
        vibrator.vibrate(new long[]{1000, 500, 100, 500}, 0);
    }

    private void initView() {
        tvTime.setText(!TextUtils.isEmpty(alarmTime) ? alarmTime.substring(0, 5) : "--:--");
        tvRemark.setText(alarmRemark);
    }

    private void initListener() {
        GlobalPublisher.setListener(GlobalPublisher.CLOSE_ALARM_CLOCK, new SignalSlot.Slot() {
            @Override
            public void onSignal(int what, int arg1, int agr2, Object argObj) {
                switch (what) {
                    case GlobalPublisher.CLOSE_ALARM_CLOCK:
                        closeAlarmClock();
                        break;
                }
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        closeAlarmClock();
    }

    @OnClick(R.id.tv_stop)
    public void onViewClicked() {
        closeAlarmClock();
    }

    private void closeAlarmClock() {
        mediaPlayer.stop();
        vibrator.cancel();
        //恢复进入闹铃前的音量设置
        if (currentVolume == 0) {
            new Thread(() -> {
                //降低音量到0
                for (int i = 0; i < 12 - currentVolume; i++) {
                    mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, 0);
                }
            }).start();
        } else if (currentVolume < 12) {
            new Thread(() -> {
                //设置音量为初始值
                mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, AudioManager.FLAG_PLAY_SOUND);
            }).start();
        }
        finish();
    }
}
